package com.campus.commons.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.InputStreamSource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Map;
@Component
public class SimpleMail implements ApplicationContextAware {

    /**
     * 发送邮件
     * @param title 邮件的主题
     * @param content 邮件的内容
     * @param to 收件人
     */
    public static void sendMail(String title, String content,String to) {
        sendMail(title, content, "", null, to,null);
    }

    /**
     * 发送邮件
     * @param title 邮件的主题
     * @param content 邮件的内容
     * @param to 收件人
     * @param mapFile 发送的附件
     */
    public static void sendMail(String title, String content,String to,Map<String,InputStreamSource> mapFile) {
        sendMail(title, content, "", null, to,mapFile);
    }

    /**
     *发送邮件
     * @param title 邮件的主题
     * @param content 邮件的内容
     * @param to 收件人
     * @param more 附带内容；  如： 可以在成功的时候：来一句你目前拥有：xxx；||以下什么:
     * @param moreArray 附带内容列表； 1， 2. 3.
     */
    public static void sendMail(String title, String content, String more,String [] moreArray,String to) {
        sendMail(title, content, more, moreArray, to,null);
    }

    /**
     * 发送邮箱
     * @param title 主题
     * @param content 内容
     * @param more 附带内容；  如： 可以在成功的时候：来一句你目前拥有：
     * @param moreArray 附带内容列表；会将每一项渲染出来；
     * @param to 接收人的邮箱
     * @param fileMap 文件集合
     */
    public static void sendMail(String title, String content, String more,String [] moreArray,String to ,Map<String, InputStreamSource> fileMap) {
        JavaMailSender bean = applicationContext.getBean(JavaMailSender.class);
        MimeMessage mimeMessage = bean.createMimeMessage();
        MimeMessageHelper mimeMessageHelper= null;
        try {
            mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
            //邮件设置值
            mimeMessageHelper.setSubject(title);//邮件主题
            mimeMessageHelper.setText("<tbody>\n" +
                            "<tr>\n" +
                            "    <td>\n" +
                            "        <style type=\"text/css\">\n" +
                            "            .qmbox body {\n" +
                            "                margin: 0 auto;\n" +
                            "                padding: 0;\n" +
                            "                font-family: Microsoft Yahei, Tahoma, Arial;\n" +
                            "                color: #333333;\n" +
                            "                background-color: #fff;\n" +
                            "                font-size: 12px;\n" +
                            "            }\n" +
                            "\n" +
                            "            .qmbox a {\n" +
                            "                color: #00a2ca;\n" +
                            "                line-height: 22px;\n" +
                            "                text-decoration: none;\n" +
                            "            }\n" +
                            "\n" +
                            "            .qmbox a:hover {\n" +
                            "                text-decoration: underline;\n" +
                            "                color: #00a2ca;\n" +
                            "            }\n" +
                            "\n" +
                            "            .qmbox td {\n" +
                            "                font-family: 'Microsoft YaHei';\n" +
                            "            }\n" +
                            "        </style>\n" +
                            "        <div><br></div>\n" +
                            "        <div>\n" +
                            "            <center>\n" +
                            "                <table align=\"center\" width=\"800\"\n" +
                            "                       style=\"font-family:'Microsoft YaHei';margin:10px 0 10px 0;background-color:#fff;color:#333333;border:1px solid #edecec;\"\n" +
                            "                       cellpadding=\"0\" cellspacing=\"0\">\n" +
                            "                    <tbody>\n" +
                            "                    <tr>\n" +
                            "                        <td>\n" +
                            "\n" +
                            "\n" +
                            "                            <div>\n" +
                            "                                <table width=\"800\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"\n" +
                            "                                       bgcolor=\"#373d41\" height=\"48\">\n" +
                            "                                    <tbody>\n" +
                            "                                    <tr>\n" +
                            "                                        <td width=\"74\" height=\"48\" border=\"0\" align=\"center\" valign=\"middle\"\n" +
                            "                                            style=\"padding-left:20px;height:48px;\">\n" +
                            "                                            <a href=\"http://www.aliyun.com/?&amp;msctype=email&amp;mscareaid=cn&amp;mscsiteid=cn&amp;mscmsgid=3460120112000540585&amp;&amp;spm=a2c4k.12424349.zh-cnt.38&amp;v=q\"\n" +
                            "                                               target=\"_blank\" rel=\"noopener\">\n" +
                            "                                                <img src=\"https://yhll.oss-cn-hangzhou.aliyuncs.com/icon.png\"\n" +
                            "                                                     height=\"48\" border=\"0\">\n" +
                            "                                            </a>\n" +
                            "                                        </td>\n" +
                            "                                        <td width=\"703\" height=\"48\" colspan=\"2\" align=\"center\" valign=\"middle\"\n" +
                            "                                            style=\"color:#ffffff; padding-right:20px;height:48px;\">\n" +
                            "                                            <a href=\"http://www.aliyun.com/?&amp;msctype=email&amp;mscareaid=cn&amp;mscsiteid=cn&amp;mscmsgid=3460120112000540585&amp;&amp;spm=a2c4k.12424349.zh-cnt.39&amp;\"\n" +
                            "                                               target=\"_blank\"\n" +
                            "                                               style=\"color:#ffffff;text-decoration:none;font-family:'Microsoft YaHei';\"\n" +
                            "                                               rel=\"noopener\">首页</a>\n" +
                            "                                            &nbsp;&nbsp;\n" +
                            "                                            <span style=\"color:#6c7479;\">|</span>&nbsp;\n" +
                            "                                            <a href=\"http://i.aliyun.com/?&amp;msctype=email&amp;mscareaid=cn&amp;mscsiteid=cn&amp;mscmsgid=3460120112000540585&amp;spm=5176.383338.201.51.1UNnoL\"\n" +
                            "                                               target=\"_blank\" style=\"color:#ffffff;text-decoration:none;\"\n" +
                            "                                               rel=\"noopener\">产品</a>\n" +
                            "                                            &nbsp;&nbsp; <span style=\"color:#6c7479;\">|</span>&nbsp;&nbsp;\n" +
                            "                                            <a href=\"http://i.aliyun.com/?&amp;msctype=email&amp;mscareaid=cn&amp;mscsiteid=cn&amp;mscmsgid=3460120112000540585&amp;spm=5176.383338.201.51.1UNnoL\"\n" +
                            "                                               target=\"_blank\" style=\"color:#ffffff;text-decoration:none;\"\n" +
                            "                                               rel=\"noopener\">反馈中心</a>\n" +
                            "                                            &nbsp;&nbsp; <span style=\"color:#6c7479;\">|</span>&nbsp;&nbsp;\n" +
                            "                                            <a href=\"http://help.aliyun.com/?&amp;msctype=email&amp;mscareaid=cn&amp;mscsiteid=cn&amp;mscmsgid=3460120112000540585&amp;&amp;spm=a2c4k.12424349.zh-cnt.43&amp;\"\n" +
                            "                                               target=\"_blank\"\n" +
                            "                                               style=\"color:#ffffff;text-decoration:none;font-family:'Microsoft YaHei';\"\n" +
                            "                                               rel=\"noopener\">帮助中心</a>\n" +
                            "                                            &nbsp;&nbsp; <span style=\"color:#6c7479;\">|</span>&nbsp;&nbsp;\n" +
                            "                                            <a href=\"https://ia.aliyun.com/home?&amp;msctype=email&amp;mscareaid=cn&amp;mscsiteid=cn&amp;mscmsgid=3460120112000540585&amp;&amp;spm=a2c4k.12424349.zh-cnt.44&amp;mark=email/\"\n" +
                            "                                               target=\"_blank\"\n" +
                            "                                               style=\"color:#ffffff;text-decoration:none;font-family:'Microsoft YaHei';\"\n" +
                            "                                               rel=\"noopener\">联系客服</a>\n" +
                            "                                        </td>\n" +
                            "                                    </tr>\n" +
                            "                                    </tbody>\n" +
                            "                                </table>\n" +
                            "                            </div>\n" +
                            "\n" +
                            "\n" +
                            "                            <div style=\"background:#FFF;padding:20px;text-align:left;\">\n" +
                            "                                <p data-spm-anchor-id=\"0.0.0.i5.7e886698Qqizbx\"\n" +
                            "                                   style=\"font-size:14px;font-weight:bold;line-height:24px;\"><span\n" +
                            "                                        style=\"color:#373d41;\">" + title + "</span></p>\n" +
                            "\n" +
                            "                                <p data-spm-anchor-id=\"0.0.0.i4.7e886698Qqizbx\"><span style=\"color:#373d41;\">" + content + "</span>\n" +
                            "                                </p>\n" +
                            "\n" +
                            "\n" +
                            "                                <p data-spm-anchor-id=\"0.0.0.i16.7e886698Qqizbx\"\n" +
                            "                                   style=\"font-size:14px;font-weight:bold;line-height:24px;\"><span\n" +
                            "                                        style=\"color:#373d41;\">" + more + "</span></p>\n" +
                            "\n" +
                            "                                <ol>\n"  +  ArrayToString(moreArray)+
                    "                                  </ol>\n" +
                            "\n" +
                            "                                <p data-spm-anchor-id=\"0.0.0.i13.7e886698Qqizbx\"><span style=\"color:#373d41;\">有任何问题,请联系相关的工作人员; 最终解释权归本系统所有...</span>\n" +
                            "                                </p>\n" +
                            "                            </div>\n" +
                            "\n" +
                            "\n" +
                            "                            <table width=\"100%\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"\n" +
                            "                                   style=\"font-size:14px;color:#333333;\">\n" +
                            "                                <tbody>\n" +
                            "                                <tr>\n" +
                            "                                    <td height=\"14\"\n" +
                            "                                        style=\"padding:0 0 16px 20px; border-bottom:1px dashed #e5e5e5;font-family:'Microsoft YaHei';\">\n" +
                            "                                        Simple Campus 平台\n" +
                            "                                    </td>\n" +
                            "                                </tr>\n" +
                            "                                <tr>\n" +
                            "                                    <td height=\"14\"\n" +
                            "                                        style=\"padding:8px 0 28px 20px;color:#999999; font-size:12px;font-family:'Microsoft YaHei';\">\n" +
                            "                                        此为系统邮件请勿回复\n" +
                            "                                    </td>\n" +
                            "                                </tr>\n" +
                            "                                </tbody>\n" +
                            "                            </table>\n" +
                            "                        </td>\n" +
                            "                    </tr>\n" +
                            "                    </tbody>\n" +
                            "                </table>\n" +
                            "            </center>\n" +
                            "\n" +
                            "            <img src=\"http://ac.mmstat.com/aliyun.12.1?logtype=4&amp;type=email&amp;msgid=3460120112000540585&amp;areaid=cn&amp;siteId=cn\">\n" +
                            "        </div>", true);//邮件内容
            if(fileMap!=null){
                for (String key:fileMap.keySet()) {
                    mimeMessageHelper.addAttachment(key,fileMap.get(key));
                }
            }
            //邮件 附件
            mimeMessageHelper.setTo(to);//邮件发给谁
            mimeMessageHelper.setFrom("920210557@qq.com"); //邮件来自于谁

        } catch (MessagingException e) {
            e.printStackTrace();
        }
        bean.send(mimeMessage);
    }



    /**
     * 将字符串数据渲染成 多个li 然后拼接；
     * @param list
     * @return
     */
    public static String ArrayToString(String [] list) {
        if (list==null) {
            return "";
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (String s : list) {
            stringBuilder.append("<li data-spm-anchor-id=\"0.0.0.i7.7e886698Qqizbx\"><span\n" +
                    " data-spm-anchor-id=\"0.0.0.i2.7e886698k3i98d\" style=\"color:#373d41;\">"+s+"</span></li>");
        }
        return stringBuilder.toString();
    }

    private static ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
      SimpleMail.applicationContext=applicationContext;
    }
}
